package cn.xo68.boot.auth.server.domain.dto;

import cn.xo68.boot.auth.server.domain.OauthRoleResource;
import cn.xo68.core.common.TreeIdGenerater;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.UUIDUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用户角色资源 传输模型
 * @author  wuxie
 * @date  2018-9-25
 */
public class OauthUserRoleResourceDto implements Serializable {

    private String roleId;
    private String clientId;
    private List<String> resourceIds;

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public List<String> getResourceIds() {
        return resourceIds;
    }

    public void setResourceIds(List<String> resourceIds) {
        this.resourceIds = resourceIds;
    }

    public List<OauthRoleResource> buildRoleResources(String operatorName, String operatorId){
        final List<String> resIds=new ArrayList<>(this.getResourceIds());
        supplementResource(resIds);

        List<OauthRoleResource> roleResources=new ArrayList<>();
        for (String resourceId: resIds) {
            OauthRoleResource roleResource=new OauthRoleResource();
            roleResource.setSerialId(UUIDUtils.createUUId());
            roleResource.setClientId(this.clientId);
            roleResource.setRoleId(this.roleId);

            roleResource.setResourceId(resourceId);

            roleResource.setOperatorId(operatorId);
            roleResource.setOperatorName(operatorName);

            roleResource.setCreateTime(DateTime.Now().getDate());
            roleResource.setUpdateTime(DateTime.Now().getDate());

            roleResources.add(roleResource);
        }
        return roleResources;
    }

    /**
     * 补齐父级资源，方便形成树型菜单
     * @param resIds
     */
    private void supplementResource(final List<String> resIds){
        Iterator<String> resIterator = resIds.iterator();

        //需要补充的编号
        final List<String> supplementIds=new ArrayList<>();

        while (resIterator.hasNext()){
            String resString = resIterator.next();
            TreeIdGenerater.TreeId treeId = TreeIdGenerater.parse(resString);
            TreeIdGenerater.TreeId parent = treeId.getParent();
            if(parent.isRoot()){
                break;
            }
            supplementResource(resIds, supplementIds, parent.asString());
        }
        resIds.addAll(supplementIds);
    }

    /**
     *  补齐父级资源，方便形成树型菜单
     * @param resIds
     * @param parentId
     */
    private void supplementResource(final List<String> resIds,final List<String> supplementIds, String parentId){
        TreeIdGenerater.TreeId parentTreeId = TreeIdGenerater.parse(parentId);
        if( !resIds.contains(parentTreeId.asString()) ){
            supplementIds.add(parentTreeId.asString());
        }
        if(!parentTreeId.getParent().isRoot()){
            supplementResource(resIds,supplementIds, parentTreeId.getParent().asString());
        }
    }
}
